package com.demo.controller;


import com.alibaba.fastjson.JSONObject;
import com.demo.pojo.User;
import com.demo.service.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/crud")
@Slf4j
public class HelloController {


    @Autowired
    private UserService userservice;

    @GetMapping("/halou")
    public String hello() {
        return "hello";
    }

    /**
     * excel 模板文件下载
     *
     * @param
     */
    @GetMapping(value = "/downloadExcel")
    public void downloadExcel(HttpServletRequest request, HttpServletResponse response) {
        String fileName = "日记账模板";

        InputStream inputStream = null;
        ServletOutputStream out = null;
        try {
            ClassPathResource ss = new ClassPathResource("mybatis-generator.xml");
            InputStream inputStream1 = ss.getInputStream();
            ClassPathResource resource = new ClassPathResource("日记账模板.xlsx");
            inputStream = resource.getInputStream();
            //inputStream = resource.getInputStream();
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            response.setContentType("application/binary;charset=UTF-8");
            response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");
            out = response.getOutputStream();
            workbook.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流信息
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * excel 模板文件下载
     *
     * @param
     */
    @GetMapping("down")
    public void downloadPermMatrix(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Workbook wb;
        InputStream inputStream = null;
        try {
            ClassPathResource resource = new ClassPathResource("物流导入.xls");
            inputStream = resource.getInputStream();
            // 根据不同excel创建不同对象,Excel2003版本-->HSSFWorkbook,Excel2007版本-->XSSFWorkbook
            wb = WorkbookFactory.create(inputStream);
            response.reset();
            response.setContentType("multipart/form-data");
            if (wb.getClass().getSimpleName() == "HSSFWorkbook") {
                response.setHeader("Content-Disposition",
                        "attachment; filename=" + new String("excel模板".getBytes(), "iso8859-1") + ".xls");
            } else {
                response.setHeader("Content-Disposition",
                        "attachment; filename=" + new String("excel模板".getBytes(), "iso8859-1") + ".xlsx");
            }
            wb.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("模板下载失败:{}",e.getMessage());
        } finally {
            //关闭流信息
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void main(String[] args) {
        Calendar cal=Calendar.getInstance();
        int i = cal.get(Calendar.HOUR_OF_DAY);
        System.out.printf("时间"+i);
        cal.setTime(new Date());
        if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){
            System.out.println("周六");
        }else {
            System.out.println("不是周六");
        }
        if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
            System.out.println("周日");
        }
    }
}